// variable library  - not a part of (document).ready
closebutton = "<div class='close'>x</div>";


function openNav (item) {
	jQuery(item).children(".subnav").fadeIn(500);
}
function closeAllNav () {
	var clearQueue = true; 
	var gotoEnd= true; 
	jQuery('div.subnav').stop(clearQueue , gotoEnd)

	jQuery('.subnav').fadeOut(500);
	jQuery("#nav li").removeClass("active")
}
function tab (tab) {
	target = jQuery(tab).attr('href').slice("1")

	jQuery('.tabbed .content').css({position:"absolute",left:"-9999px"})
	jQuery('#'+target).show().css({position:"relative",left:"0"})
	jQuery('.tabbed .nav li').removeClass("active")
	jQuery(tab).parent("li").addClass("active")
}



jQuery(document).ready(function($) {
	// navigation section
	jQuery('.subnav').css({left: '0', width: 'auto'})

try {
	jQuery('.subnav').each(function() {  //calculates the subnav width by content width and assigns to each
		var ulwidth = 0;
		var h2width = jQuery(this).children("h2").width() + 15;
		var h3width = jQuery(this).children("h3").width() + 15;
		jQuery(this).children('ul,p').each(function() {
			if (jQuery(this).width() < 160) {
				jQuery(this).css('width', "160px")
			};
			ulwidth = ulwidth + jQuery(this).width() + 31;
		});
		if (ulwidth > h2width && ulwidth > h3width ) var subnavwidth	= ulwidth;
		else if (h2width > h3width ) var subnavwidth	= h2width;
		else if (h3width >= h2width ) var subnavwidth	= h3width;
		jQuery(this).css('width', subnavwidth+"px")
	});

} catch (err) {
	//if(window.console) { window.console.log(err) }	
	return false;
}

		jQuery('.subnav').hide().prepend(closebutton);

		jQuery('#nav .close').click(function() {
			closeAllNav();	
		});

		jQuery("#nav li a.section").parent().hover(function() {
			jQuery(this).addClass('active');
			openNav(jQuery(this))
		}, function () {
			closeAllNav();
		});

		jQuery('.subnav').each(function(index) {
			jQuery(this).children("ul:first").addClass('first');
			jQuery(this).children("ul:last").addClass('last');
		});

		
		
try {
		if (jQuery('.tabbed ').size() > 0) {

			tab(jQuery(".tabbed  .nav li:eq(0) a"))
			jQuery('.tabbed  .nav li a').click(function() {
				tab(jQuery(this))
				return false;
			});
		};

} catch (err) {
	//if(window.console) { window.console.log(err) }	
	return false;
}

		
		
	
});
